<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * pelamar.php
 * @author ranuandra
 *
 * 1 Mei 2012 00:34:27
 *
 * 
 */

class Controller_Admin_Pelamar extends Kohana_Admin
{
	
	public function action_index()
	{
		$datas = ORM::factory('mstpelamar')->order_by('nama_lengkap', 'ASC')->find_all();
		$view = View::factory('admin/pelamar/list');
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_apply()
	{
		
		$id = Request::current()->param('id');
		$datas = ORM::factory('pelamarlowongan')->where('mstpelamar_id', '=', $id)->find_all();	
		$view = View::factory('admin/pelamar/lowonganlist');	
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_lowongan()
	{
		$id = Request::current()->param('id');
		$lowongan = ORM::factory('mstlowongan', $id);
		$pelamars = $lowongan->pelamarlowongan->where('flag_reject', '=', 0)->find_all();
		//print_r($pelamars);
		$view = View::factory('admin/pelamar/index');
		$view->id = $id;
		$view->lowongan = $lowongan;
		$view->datas = $pelamars;
		$this->template->container = $view;
		
	}
	
	public function action_rejectconfirm()
	{
		$datas = array();
		//$this->auto_render = false;
		$pelamars = Request::current()->post('panggil');
		$id = Request::current()->post('mstlowongan_id');
		$lowongan = ORM::factory('mstlowongan')->where('id', '=', $id)->find();
		
		foreach($pelamars as $pelamar)
		{
			$datas[] = ORM::factory('pelamarlowongan', $pelamar);
		}
		$view = View::factory('admin/pelamar/reject');
		$view->lowongan = $lowongan;
		$view->id = $id;
		$view->proseses = ORM::factory('mstproses')->find_all()->as_array('id', 'proses');
		$view->pelamarlowongan_id = $pelamars;
		$view->deskripsi = $lowongan->deskripsi;
		$view->datas = $datas;
		if(Request::current()->is_ajax() == $this->request)
		{
			$this->auto_render = false;
			echo $view;
			return;
		}
		//echo $view;
		$this->template->container = $view;
	}
	
	public function action_reject()
	{
		if($_POST)
		{
			$db = Database::instance();
			$db->begin();
			try 
			{
				
				$pelamars = Request::current()->post('pelamarlowongan_id');
				//kirim email kepada yg dipanggil
				$proses = ORM::factory('mstproses',Request::current()->post('mstproses_id') );
				foreach($pelamars as $pelamar)
				{	
					$datapelamar = ORM::factory('pelamarlowongan', $pelamar);
					$datapelamar->flag_reject = 1;
					$datapelamar->update();
					//mail($user->email,$subject,$message,$headers);
					
				}	

				$db->commit();
				Request::current()->redirect(URL::base()."admin/pelamar/lowongan/".Request::current()->post('mstlowongan_id'));
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);				
			}
		}				
	}
	
	public function action_detail()
	{
		$this->auto_render = false;
		$id = Request::current()->param("id");
		if($id)
		{
			$data = ORM::factory('pelamarlowongan', $id);
			//print_r($data);
			$view = View::factory('admin/pelamar/detail');
			//$view->id = $id;
			$view->data = $data;
			if(Request::current()->is_ajax() == $this->request)
			{
				$this->auto_render = false;
				echo $view;
				return;
			}
			
			$this->template->container = $view;
			
		}
	}
	
	public function action_confirm()
	{
		$datas = array();
		//$this->auto_render = false;
		$pelamars = Request::current()->post('panggil');

		
		foreach($pelamars as $pelamar)
		{
			$datas[] = ORM::factory('pelamarlowongan', $pelamar);
		}
		
		$lowongan = ORM::factory('mstlowongan', $datas[0]->mstlowongan->id);
		
		$view = View::factory('admin/pelamar/confirm');
		$view->lowongan = $lowongan;
		$view->id = $datas[0]->mstlowongan->id;
		$view->proseses = ORM::factory('mstproses')->find_all()->as_array('id', 'proses');
		$view->pelamarlowongan_id = $pelamars;
		$view->datas = $datas;
		if(Request::current()->is_ajax() == $this->request)
		{
				$this->auto_render = false;
				echo $view;
				return;			
		}
		
		//echo $view;
		$this->template->container = $view;
		
	}
	
	public function action_proses()
	{
		if($_POST)
		{
			$db = Database::instance();
			$db->begin();
			try 
			{
				$year = date("Y");
				$month = date("m");
				$mx = DB::select(array('MAX("id")', 'maks'))->from('proseslowongans')->execute();

				$no = $mx[0]['maks'] + 1;

				$proses = ORM::factory('proseslowongan');
				$proses->kode = "PRS-".$year.$month."-".$no;
				$proses->tanggal = Request::current()->post('tgl')." ".Request::current()->post('jam');
				$proses->create_data($_POST);
				$prosesid = $proses->id;
				
				
				$pelamars = Request::current()->post('pelamarlowongan_id');
				
				//simpan pelamar untuk proses selanjutnya
				foreach($pelamars as $pelamar)
				{
					$pelamarproses = ORM::factory('pelamarproses');
					$pelamarproses->pelamarlowongan_id = $pelamar;
					$pelamarproses->proseslowongan_id = $prosesid;
/*					$pelamarproses->flag_hadir = 1;
					$pelamarptoses->flaglolos = 1;
					$pelamarproses->flag_hadir = 1;*/
					$pelamarproses->save();
					
					//tandai yg lolos ke proses selanjutnya
/*					$ceklolos = ORM::factory('pelamarproses', $pelamar);
					$ceklolos->flag_panggil = 1;
					$ceklolos->update();*/
					//tandai yg dipanggil
					$pelamar = ORM::factory('pelamarlowongan', $pelamar);
					$pelamar->flag_panggil = 1;
					$pelamar->update();
				}
				
				
				if(isset($_POST['mstlowongan_id']))
				{
					$lowongan = ORM::factory('mstlowongan', Request::current()->post('mstlowongan_id'));
					$lowongan->mstproses_id = Request::current()->post('mstproses_id');
					$lowongan->update();
				}
				
				$db->commit();
				//kirim email kepada yg dipanggil
				$proses = ORM::factory('mstproses',Request::current()->post('mstproses_id') );
				foreach($pelamars as $pelamar)
				{	
					$datapelamar = ORM::factory('pelamarlowongan', $pelamar);
					$userid = $datapelamar->mstpelamar->user_id;
					$user = ORM::factory('user', $userid);

					$subject = "Panggilan ". $proses->proses;
					$message = "Anda diminta untuk mengikuti proses ".$proses->proses." dengan data:\n";
					$message .= "Tanggal : ".Request::current()->post('tgl')."\n";
					$message .= "Jam : ".Request::current()->post('jam')."\n";
					$message .= "Lokasi : ".Request::current()->post('alamat')."\n";
					$from = "hrd@nasmoco.com";
					$headers = "From:" . $from;
					mail($user->email,$subject,$message,$headers);
					
				}

				Request::current()->redirect(URL::base()."admin/pelamar/lowongan/".Request::current()->post('mstlowongan_id'));
				
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);				
			}
		}		
	}
}